package gentle.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import gentle.base.SqlMapper;
import gentle.base.StringUtil;
import gentle.entity.CustomerInfo;
import gentle.entity.ReturnVisitInfo;
import gentle.entity.SearchReturnVisitInfo;
import gentle.mapper.ConsultantDiagnosisDao;
import gentle.service.IConsultantDiagnosisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class ConsultantDiagnosisServiceImpl implements IConsultantDiagnosisService {
    @Autowired
    private ConsultantDiagnosisDao dao;
    @Autowired
    private SqlMapper sqlMapper;

    @Override
    public List<ReturnVisitInfo> selectPageConsultantByQuery(Page<ReturnVisitInfo> page, SearchReturnVisitInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        String tableHead = searchInfo.getAbbreviation();
        if (StringUtil.isNotEmpty(searchInfo.getOutpatientName())){
            params.put("outpatientName", searchInfo.getOutpatientName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getSreach())) {
            params.put("sreach", searchInfo.getSreach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getFieldTc())) {
            params.put("fieldTc", searchInfo.getFieldTc());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartDiagnosisTime())) {
            params.put("startDiagnosisTime", searchInfo.getStartDiagnosisTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndDiagnosisTime())) {
            params.put("endDiagnosisTime", searchInfo.getEndDiagnosisTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getFollowStatus())) {
            params.put("followStatus", searchInfo.getFollowStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getConsumptionStatus())) {
            params.put("consumptionStatus", searchInfo.getConsumptionStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getTcName())) {
            params.put("tcName", searchInfo.getTcName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDealStatus())) {
            if ("已结账".equals(searchInfo.getDealStatus())) {
                params.put("dealStatus", "a.deal_status ='已结账'");
            } else {
                params.put("dealStatus", "a.deal_status !='已结账'");
            }
        }

        params.put("tableName", tableHead + "customer_info");
        return dao.getQueryPage2(page, params);
    }

    @Override
    public int getQueryPagesCount(Page<ReturnVisitInfo> page, SearchReturnVisitInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        String tableHead = searchInfo.getAbbreviation();
        if (StringUtil.isNotEmpty(searchInfo.getOutpatientName())){
            params.put("outpatientName", searchInfo.getOutpatientName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getSreach())) {
            params.put("sreach", searchInfo.getSreach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getFieldTc())) {
            params.put("fieldTc", searchInfo.getFieldTc());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartDiagnosisTime())) {
            params.put("startDiagnosisTime", searchInfo.getStartDiagnosisTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndDiagnosisTime())) {
            params.put("endDiagnosisTime", searchInfo.getEndDiagnosisTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getFollowStatus())) {
            params.put("followStatus", searchInfo.getFollowStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getConsumptionStatus())) {
            params.put("consumptionStatus", searchInfo.getConsumptionStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getTcName())) {
            params.put("tcName", searchInfo.getTcName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDealStatus())) {
            if ("已结账".equals(searchInfo.getDealStatus())) {
                params.put("dealStatus", "a.deal_status ='已结账'");
            } else {
                params.put("dealStatus", "a.deal_status !='已结账'");
            }
        }
        params.put("tableName", tableHead + "customer_info");
        return dao.getQueryPagesCount(page, params);
    }

    @Override
    public int delete_tcDiagnosis(String id,String reservationsId, String abbreviation){
        String ctable=abbreviation+"customer_info";
        String sql = "delete from tc_diagnosis_info  where id = '" + id + "'";
        sqlMapper.delete(sql);
        String sql1="select * from tc_diagnosis_info where reservations_id='"+reservationsId+"';";
        List<CustomerInfo> list=sqlMapper.selectList(sql1,CustomerInfo.class);
        if(list.size()>0) {
            String sql2 = "update " + ctable + " set diagnosis_status='已看诊' where reservations_id='" + reservationsId + "'";
            return sqlMapper.update(sql2);
        }else {
            String sql2 = "update " + ctable + " set diagnosis_status='未看诊' where reservations_id='" + reservationsId + "'";
            return sqlMapper.update(sql2);
        }
    }

    @Override
    public int update_tcDiagnosisOk(String id, String diagnosisContent){
        String sql = "update tc_diagnosis_info set diagnosis_content='" + diagnosisContent + "'" +
                " where id='" + id + "' ";
        return sqlMapper.update(sql);
    }
}